Page({
  data: {
    keywords: '',
    categories: [
      { id: 'all', name: '全部' },
  // 全局加载状态
  globalLoading: false,
  globalLoadingText: '服务事项加载中...',

  // 显示全局加载
  showGlobalLoading(text = '服务事项加载中...') {
    const app = getApp()
    app.showGlobalLoading({
      loadingText: text
    })
  },

  // 隐藏全局加载
  hideGlobalLoading() {
    const app = getApp()
    app.hideGlobalLoading()
  },

  // 更新全局加载状态
  updateGlobalLoading(data) {
    this.setData({
      globalLoading: data.globalLoading,
      globalLoadingText: data.globalLoadingText || this.data.globalLoadingText
    })
  },
      { id: 'conflict', name: '纠纷冲突' },
      { id: 'theft', name: '盗窃破坏' },
      { id: 'gambling', name: '赌博聚众' },
      { id: 'fraud', name: '诈骗线索' },
      { id: 'other', name: '其他' }
    ],
    currentCateId: 'all',
    cateCounts: {},
    filterOptions: [
      { id: 'online', name: '可线上' },
      { id: 'appoint', name: '需预约' }
    ],
    filter: {},
    items: [
      {
        id: 'i1',
        title: '邻里纠纷调处',
        desc: '对邻里矛盾、口角冲突进行登记、调解与跟踪。',
        cateId: 'conflict',
        online: true,
        appoint: false,
        dept: '综合治理中心'
      },
      {
        id: 'i2',
        title: '盗窃破坏线索上报',
        desc: '涉及盗窃、毁坏公共财物等情况的线索收集与上报。',
        cateId: 'theft',
        online: true,
        appoint: false,
        dept: '派出所'
      },
      {
        id: 'i3',
        title: '聚众赌博举报',
        desc: '对聚众赌博等违法行为进行举报与取证说明。',
        cateId: 'gambling',
        online: false,
        appoint: true,
        dept: '派出所'
      },
      {
        id: 'i4',
        title: '电信网络诈骗线索',
        desc: '可疑电话、链接、群聊等诈骗线索的收集与报送。',
        cateId: 'fraud',
        online: true,
        appoint: false,
        dept: '反诈工作站'
      }
    ],
    filteredItems: [],
    cateMap: {},
    favorites: {}
  },

  onLoad() {
    this.buildCateMap();
    this.updateCounts();
    this.applyFilter();
  },

  buildCateMap() {
    const cateMap = {};
    this.data.categories.forEach(c => {
      cateMap[c.id] = c.name;
    });
    this.setData({ cateMap });
  },

  updateCounts() {
    const counts = {};
    this.data.items.forEach(it => {
      counts[it.cateId] = (counts[it.cateId] || 0) + 1;
    });
    counts.all = this.data.items.length;
    this.setData({ cateCounts: counts });
  },

  onSearchConfirm(e) {
    const val = (e.detail.value || '').trim();
    this.setData({ keywords: val }, this.applyFilter);
  },

  onTapCate(e) {
    const id = e.currentTarget.dataset.id;
    this.setData({ currentCateId: id }, this.applyFilter);
  },

  onToggleFilter(e) {
    const type = e.currentTarget.dataset.type;
    const next = Object.assign({}, this.data.filter, { [type]: !this.data.filter[type] });
    this.setData({ filter: next }, this.applyFilter);
  },

  applyFilter() {
    const { items, currentCateId, keywords, filter } = this.data;
    const kw = (keywords || '').toLowerCase();
    const wantOnline = !!filter.online;
    const wantAppoint = !!filter.appoint;

    const inCate = it => currentCateId === 'all' || it.cateId === currentCateId;
    const byKw = it => !kw || it.title.toLowerCase().includes(kw) || it.desc.toLowerCase().includes(kw);
    const byTag = it => (!wantOnline || it.online) && (!wantAppoint || it.appoint);

    const filtered = items.filter(it => inCate(it) && byKw(it) && byTag(it));
    this.setData({ filteredItems: filtered });
  },

  onToggleFav(e) {
    e.stopPropagation && e.stopPropagation();
    const id = e.currentTarget.dataset.id;
    const next = Object.assign({}, this.data.favorites, { [id]: !this.data.favorites[id] });
    this.setData({ favorites: next });
  },

  onTapItem(e) {
    const id = e.currentTarget.dataset.id;
    // 预留：跳转到具体上报流程
    wx.showToast({ title: '进入上报：' + id, icon: 'none' });
  }
});


